package com.flea.hclv.service.impl;

import com.alibaba.fastjson.JSON;
import com.flea.hclv.constant.Const;
import com.flea.hclv.constant.HConstant;
import com.flea.hclv.model.dao.*;
import com.flea.hclv.model.entity.*;
import com.flea.hclv.service.HGuideService;
import com.flea.hclv.util.EmailUtil;
import com.flea.hclv.util.ServerResponse;
import com.flea.system.bean.FleaPage;
import com.flea.system.dao.OrgRoleMapper;
import com.flea.system.dao.WebDictMapper;
import com.flea.system.entity.OrgDept;
import com.flea.system.entity.OrgRole;
import com.flea.system.entity.WebDict;
import com.flea.system.platform.session.FleaOrgUtils;
import com.flea.system.utils.FleaStringUtils;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: project
 * @description: 导游管理
 * @author: shiliqiang
 * @create: 2018-11-13 14:45
 * @version: 1.0
 */
@Service
public class HGuideServiceImpl implements HGuideService {

//    private Logger logger = LoggerFactory.getLogger(HGuideServiceImpl.class);

    @Autowired
    private HGuideMapper hGuideMapper;
    @Autowired
    private HAddressMapper hAddressMapper;
    @Autowired
    private WebDictMapper webDictMapper;
    @Autowired
    private HGuideTravelGroupMapper hGuideTravelGroupMapper;
    @Autowired
    private HTravelGroupMapper hTravelGroupMapper;
    @Autowired
    private HGuideAccountMapper hGuideAccountMapper;
    @Autowired
    private OrgRoleMapper orgRoleMapper;

    @Override
    public ServerResponse<String> updateStatusByGuideId(String guideId,String userId,String status) {
        HGuide hGuide = hGuideMapper.selectByPrimaryKey(guideId);
        if (hGuide==null){
            return ServerResponse.createByErrorMessage("没有找到导游信息");
        }
        hGuide.setModifyUser(userId);
        hGuide.setStatus(status);
        int row = hGuideMapper.updateByPrimaryKeySelective(hGuide);
        if (row==1){
            return ServerResponse.createBySuccessMessage("更新成功");
        }
        return ServerResponse.createByErrorMessage("更新失败");
    }

    @Override
    public ServerResponse<String> deleteGuideGroupRelation(String groupId,String userId) {
        if (StringUtils.isEmpty(groupId)){
            return ServerResponse.createByErrorMessage("团id为空");
        }
        HTravelGroup group = hTravelGroupMapper.selectByPrimaryKey(groupId);
        if (!StringUtils.equals(group.getStatus(),HConstant.HCLV_VISITOR_GROUP_ISNO) &&
                !StringUtils.equals(group.getStatus(),HConstant.HCLV_VISITOR_GROUP_ISYES)){
            return ServerResponse.createByErrorMessage("团状态为回团，关闭团不可以取消指派");
        }
        HGuideTravelGroup entity = hGuideTravelGroupMapper.selectByTravelGroupId(groupId);
        if (entity==null){
            return ServerResponse.createByErrorMessage("没有找到团导游关联信息");
        }

        int row = hGuideTravelGroupMapper.deleteByPrimaryKey(entity.getId());
        if (row==1){
//            String guideId = entity.getGuideId();
//            entity = hGuideTravelGroupMapper.selectFirstNewByGuideId(guideId);
//            if (entity == null) {
//                return ServerResponse.createBySuccessMessage("没有找到最近一次的接团信息");
//            }
//            HTravelGroup group = hTravelGroupMapper.selectByPrimaryKey(entity.getTravelGroupId());
//            if (group==null){
//                return ServerResponse.createByErrorMessage("没有找到团信息");
//            }
//            //导游已经接的团中有未出团，不需要更新状态
//            if (!StringUtils.equals(group.getStatus(),HConstant.HCLV_VISITOR_GROUP_ISNO)){
//                return ServerResponse.createBySuccess();
//            }
//            //更新导游状态
//            return updateStatusByGuideId(guideId,userId,HConstant.D_GUIDE_STATUS_ON_LINE);
            return ServerResponse.createBySuccessMessage("取消成功");
        }
        return ServerResponse.createByErrorMessage("取消失败");

    }

    @Override
    public ServerResponse<HGuide> getGuideInfoByUserId(String userId) {
        return getGuideInfo(null,userId);
    }

    @Override
    public ServerResponse<String> updateOperateStatus(String userId, String guideId, String status) {
        HGuide hGuide = hGuideMapper.selectByPrimaryKey(guideId);
        if (hGuide == null) {
            return ServerResponse.createByErrorMessage("没有找到导游信息");
        }
        hGuide.setAuditStatus(status);
        hGuide.setModifyUser(userId);
        int row = hGuideMapper.updateByPrimaryKeySelective(hGuide);
        if (row == 1) {
            return ServerResponse.createBySuccessMessage("更新状态成功");
        }
        return ServerResponse.createByErrorMessage("更新状态失败");
    }

    @Override
    public ServerResponse<String> updateGuideInfo(String userId, String model) {
        if (StringUtils.isBlank(model)) {
            return ServerResponse.createByErrorMessage("model为空");
        }

        //导游的出发地
        HAddress hAddress = JSON.parseObject(model, HAddress.class);
        String province = hAddress.getProvince();
        if (StringUtils.isEmpty(province)) {
            return ServerResponse.createByErrorMessage("省份为空");
        }
        String city = hAddress.getCity();
        if (StringUtils.isEmpty(city)) {
            return ServerResponse.createByErrorMessage("城市为空");
        }

        //导游其他信息
        HGuide hGuide = JSON.parseObject(model, HGuide.class);
        String guideId = hGuide.getId();
        if (StringUtils.isEmpty(guideId)) {
            return ServerResponse.createByErrorMessage("导游ID为空");
        }


        HGuide guide = hGuideMapper.selectByPrimaryKey(guideId);
        if (guide == null) {
            return ServerResponse.createByErrorMessage("未找到改导游的信息，请确认id是否正确");
        }
        String addressId = guide.getRegisterArea();
        if (StringUtils.isEmpty(addressId)) {
            return ServerResponse.createByErrorMessage("导游出发地id为空");
        }
        HAddress address = hAddressMapper.selectByPrimaryKey(addressId);
        if (address == null) {
            return ServerResponse.createByErrorMessage("没有找到导游出发地信息");
        }
        if (!StringUtils.equals(address.getProvince(),hGuide.getProvince())||!StringUtils.equals(address.getCity(),hGuide.getCity())){
            address.setProvince(province);
            address.setCity(city);
            address.setModifyUser(userId);
            int row = hAddressMapper.updateByPrimaryKeySelective(address);
            if (row != 1) {
                return ServerResponse.createByErrorMessage("导游地址更新失败");
            }
        }
        hGuide.setModifyUser(userId);
        // 设置审核状态 未审核
        hGuide.setAuditStatus(Const.GuideAuditStatusEnum.UN_AUDIT.getCode());
        int row = hGuideMapper.updateByPrimaryKeySelective(hGuide);
        if (row != 1) {
            return ServerResponse.createByErrorMessage("导游相关信息更新失败");
        }
        return ServerResponse.createBySuccessMessage("审核信息已提交");
    }

    @Override
    public ServerResponse<FleaPage<HGuide>> selectUnaudited(int curPage, int pageSize, String un_auditCode) {
        RowBounds rowBounds = new RowBounds(curPage, pageSize);
        List<HGuide> hGuides = hGuideMapper.selectAll(rowBounds, null, null, un_auditCode, null);
        FleaPage<HGuide> fleaPage = new FleaPage<>(hGuides);
        return ServerResponse.createBySuccess(fleaPage);
    }

    @Override
    public ServerResponse<FleaPage<HGuideTravelGroup>> getGuideTravelGroupPayInfo(String userId, int curPage, int pageSize, String status, String groupName, String guideName, String guidePhone) {
        HGuide hGuide = hGuideMapper.selectByUserId(userId);
        RowBounds rowBounds = new RowBounds(curPage, pageSize);
        List<HGuideTravelGroup> hGuideTravelGroupList;
        int roleCode = 0;  // 角色  1 导游管理员 2.组团社 3导游
        //获取用户部门
        OrgDept dept = FleaOrgUtils.getCurrentUserDept(userId);
        if (dept==null){
            return ServerResponse.createByErrorMessage("部门不能为空");
        }
        String serviceType = dept.getServiceType();
        //获取当前用户角色
        List<OrgRole> orgRoles = orgRoleMapper.selectByUserUuid(userId);
        for (OrgRole role : orgRoles) {
            String roleCode1 = role.getRoleCode();
            if (roleCode1.equals(HConstant.HCLV_GAID_ADMIN)){
                roleCode = 1;
                break;
            }
        }
        //如果是导游管理员，查询所有
        if (roleCode==1){
            hGuideTravelGroupList = hGuideTravelGroupMapper.selectAll(rowBounds);
            if (CollectionUtils.isEmpty(hGuideTravelGroupList)) {
                return ServerResponse.createBySuccess(new FleaPage<HGuideTravelGroup>());
            }
            FleaPage<HGuideTravelGroup> temp = new FleaPage<>(hGuideTravelGroupList);
            int allDataCount = (int) temp.getAllDataCount();

            List<HGuideTravelGroup> list = assembleHGuideTravelGroup(hGuideTravelGroupList,roleCode);
            FleaPage<HGuideTravelGroup> data = new FleaPage<>();
            data.setPageSize(pageSize);
            data.setCurPage(curPage);
            data.setAllDataCount(allDataCount);
            data.setCurPageData(list);
            return ServerResponse.createBySuccess(data);
        }
        for (OrgRole role : orgRoles) {
            String roleCode1 = role.getRoleCode();
            if (roleCode1.equals(HConstant.HCLV_GUAI_CONFIRM_YES_ROLE)){
                roleCode = 3;
                break;
            }
        }
        if (roleCode==3 && hGuide!=null){
            hGuideTravelGroupList = hGuideTravelGroupMapper.selectByGuideIdAndPage(hGuide.getId(), rowBounds);
            if (CollectionUtils.isEmpty(hGuideTravelGroupList)) {
                return ServerResponse.createBySuccess(new FleaPage<HGuideTravelGroup>());
            }
            FleaPage<HGuideTravelGroup> temp = new FleaPage<>(hGuideTravelGroupList);
            int allDataCount = (int) temp.getAllDataCount();

            List<HGuideTravelGroup> list = assembleHGuideTravelGroup(hGuideTravelGroupList,roleCode);
            FleaPage<HGuideTravelGroup> data = new FleaPage<>();
            data.setPageSize(pageSize);
            data.setCurPage(curPage);
            data.setAllDataCount(allDataCount);
            data.setCurPageData(list);
            return ServerResponse.createBySuccess(data);
        }
        //组团社
        if (serviceType.equals(HConstant.D_SERVICE_ORG_GROUP) || serviceType.equals(HConstant.D_SERVICE_ORG_SUPPLIER)){
            roleCode=2;
            // 1.获取当前部门(组团社)的所有团
            List<HTravelGroup> groupList = hTravelGroupMapper.selectByDeptIdAndGroupName(null,groupName);
            if (CollectionUtils.isEmpty(groupList)) {
                return ServerResponse.createBySuccess(new FleaPage<HGuideTravelGroup>());
            }
            List<HGuide> guides=null;
            if (StringUtils.isNotBlank(guideName) || StringUtils.isNotBlank(guidePhone)) {
                guides = hGuideMapper.selectByNameAndPhone(guideName,guidePhone);
                if (CollectionUtils.isEmpty(guides)) {
                    return ServerResponse.createBySuccess(new FleaPage<HGuideTravelGroup>());
                }
            }
            // 2.过滤团，获取所有已经指派导游的团_导游关联信息
            hGuideTravelGroupList = hGuideTravelGroupMapper.selectByGroupList(dept.getDeptUuid(),status,guides,groupList,rowBounds);
            FleaPage<HGuideTravelGroup> temp = new FleaPage<>(hGuideTravelGroupList);
            int allDataCount = (int) temp.getAllDataCount();

            List<HGuideTravelGroup> list = assembleHGuideTravelGroup(hGuideTravelGroupList,roleCode);
            FleaPage<HGuideTravelGroup> data = new FleaPage<>();
            data.setPageSize(pageSize);
            data.setCurPage(curPage);
            data.setAllDataCount(allDataCount);
            data.setCurPageData(list);
            return ServerResponse.createBySuccess(data);
        }
        return ServerResponse.createByErrorMessage("没有权限");
    }

    private List<HGuideTravelGroup> assembleHGuideTravelGroup(List<HGuideTravelGroup> hGuideTravelGroupList, int roleCode) {
        List<HGuideTravelGroup> entityList = Lists.newArrayList();
        for (HGuideTravelGroup entity : hGuideTravelGroupList) {
            //获取团名称
            String travelGroupId = entity.getTravelGroupId();
            HGuide hGuide = hGuideMapper.selectByPrimaryKey(entity.getGuideId());
            if (StringUtils.isEmpty(travelGroupId)) {
                return null;
            }
            HTravelGroup group = hTravelGroupMapper.selectByPrimaryKey(travelGroupId);
            //获取导游姓名
            String guideName = hGuide.getGuideName();
            entity.setRoleCode(roleCode);
            entity.setGuideName(guideName);
            entity.setGuidePhone(hGuide.getGuidePhone());
            entity.setTravelGroupName(group == null ? null : group.getGroupName());
            entityList.add(entity);
        }
        return entityList;
    }

    @Override
    public ServerResponse<HGuide> getGuideInfo(String guideId,String userId) {
        HGuide hGuide;
        if (userId!=null){
            hGuide = hGuideMapper.selectByUserId(userId);
        }else {
            hGuide = hGuideMapper.selectByPrimaryKey(guideId);
        }

        if (hGuide == null) {
            return ServerResponse.createByErrorMessage("请提交导游审核信息");
        }
        String addressId = hGuide.getRegisterArea();
        if (StringUtils.isEmpty(addressId)) {
            return ServerResponse.createByErrorMessage("没有找到该导游的地址信息");
        }
        HAddress address = hAddressMapper.selectByPrimaryKey(addressId);
        if (address == null) {
            return ServerResponse.createByErrorMessage("该地址不存在");
        }
        String provinceCode = address.getProvince();
        //获取字典表code的名称
        WebDict city = webDictMapper.findByCodeAndType(provinceCode, Const.D_REGION);
        // D_REGION
        if (city == null) {
            return ServerResponse.createByErrorMessage("没找到字典信息");
        }
        hGuide.setProvinceName(city.getDictName());

        String cityCode = address.getCity();
        WebDict provice = webDictMapper.findByCodeAndType(cityCode, Const.D_REGION);
        if (provice == null) {
            return ServerResponse.createByErrorMessage("没找到字典信息");
        }
        hGuide.setCityName(provice.getDictName());
        hGuide.setProvince(provinceCode);
        hGuide.setCity(cityCode);
        return ServerResponse.createBySuccess(hGuide);
    }

    @Override
    public ServerResponse<String> saveGuideGroupPayInfo(String userId, HGuideTravelGroup hGuideTravelGroup) {
        String id = hGuideTravelGroup.getId();
        if (StringUtils.isBlank(id)) {
            return ServerResponse.createByErrorMessage("导游——团Id为空");
        }
        HGuideTravelGroup entity = hGuideTravelGroupMapper.selectByPrimaryKey(id);
        if (entity==null){
            return ServerResponse.createByErrorMessage("没有找到记录");
        }
        String status = entity.getStatus();
        if (StringUtils.equals(status,HConstant.D_GUIDE_PAY_STATUS_YES)){
            return ServerResponse.createByErrorMessage("已经支付，请不要重复支付");
        }
        HTravelGroup group = hTravelGroupMapper.selectByPrimaryKey(entity.getTravelGroupId());
        if (group==null){
            return ServerResponse.createByErrorMessage("团没有找到");
        }
        String groupStatus = group.getStatus();
        if (StringUtils.equals(groupStatus,HConstant.HCLV_VISITOR_GROUP_ISNO) || StringUtils.equals(groupStatus,HConstant.HCLV_VISITOR_GROUP_ISYES)){
            return ServerResponse.createByErrorMessage("回团，团关闭状态才可以支付");
        }
        if (hGuideTravelGroup.getPayMoney().isNaN()) {
            return ServerResponse.createByErrorMessage("支付金额不能空");
        }
        if (StringUtils.isBlank(hGuideTravelGroup.getPlatformNumber())) {
            return ServerResponse.createByErrorMessage("支付账号不能为空");
        }
        hGuideTravelGroup.setModifyUser(userId);
        hGuideTravelGroup.setStatus(HConstant.D_GUIDE_PAY_STATUS_YES);
        int row = hGuideTravelGroupMapper.updateByPrimaryKeySelective(hGuideTravelGroup);
        if (row == 1) {
            return ServerResponse.createBySuccessMessage("支付成功");
        }
        return ServerResponse.createByErrorMessage("支付失败");
    }

    @Override
    public ServerResponse<String> saveGuideGroup(String userId, String groupId, String guideId, String payDeptId, Float guideCost) {
        if (StringUtils.isBlank(groupId)) {
            return ServerResponse.createByErrorMessage("团ID为空");
        }
        if (StringUtils.isBlank(guideId)) {
            return ServerResponse.createByErrorMessage("导游ID为空");
        }
        if (StringUtils.isBlank(payDeptId)) {
            return ServerResponse.createByErrorMessage("支付部门ID为空");
        }
        if (guideCost==null) {
            return ServerResponse.createByErrorMessage("支付费用为空");
        }

        //判断团状态
        HTravelGroup group = hTravelGroupMapper.selectByPrimaryKey(groupId);
        if (group==null){
            return ServerResponse.createByErrorMessage("没有找到团");
        }
        //出团中
//        if (StringUtils.equals(group.getStatus(),HConstant.HCLV_VISITOR_GROUP_ISYES)){
//            return ServerResponse.createByErrorMessage("该团正在出团中");
//        }
        //回团
        if (StringUtils.equals(group.getStatus(),HConstant.HCLV_VISITOR_GROUP_ISREBACK)){
            return ServerResponse.createByErrorMessage("该团已经回团");
        }
        //关闭团
        if (StringUtils.equals(group.getStatus(),HConstant.HCLV_VISITOR_GROUP_ISCLOSE)){
            return ServerResponse.createByErrorMessage("该团已经关闭");
        }

        //判断团是否已经被指派
        HGuideTravelGroup guideTravelGroup = hGuideTravelGroupMapper.selectByTravelGroupId(groupId);
        if (guideTravelGroup != null) {
            return ServerResponse.createByErrorMessage("该团已经指派导游");
        }

        //进入未指派判断
        //判断导游状态是否在服务中 或休息中
        HGuide guide = hGuideMapper.selectByPrimaryKey(guideId);
        if (guide==null){
            return ServerResponse.createByErrorMessage("没有找到导游信息");
        }
        //判断审核状态
        if (!StringUtils.equals(guide.getAuditStatus(),Const.GuideAuditStatusEnum.PASSED.getCode())){
            return ServerResponse.createByErrorMessage("导游审核未通过");
        }
//        String guideStatus = guide.getStatus();
//        switch (guideStatus){
//            //导游出团中
//            case HConstant.D_GUIDE_STATUS_WORKING :
//                return ServerResponse.createByErrorMessage("出团中");
//            //导游休息中
//            case HConstant.D_GUIDE_STATUS_REST :
//                return ServerResponse.createByErrorMessage("导游休息中");
//            //导游已接团没未出团
//            case HConstant.D_GUIDE_STATUS_RECEIVE :
//                //获取导游最近一次接的团。判断，可以指派同一班期的团
//                guideTravelGroup = hGuideTravelGroupMapper.selectFirstNewByGuideId(guideId);
//                if (guideTravelGroup == null) {
//                    return ServerResponse.createBySuccessMessage("没有找到最近一次的接团信息");
//                }
//                HTravelGroup group = hTravelGroupMapper.selectByPrimaryKey(guideTravelGroup.getTravelGroupId());
//                if (group==null){
//                    return ServerResponse.createByErrorMessage("没有找到团信息");
//                }
//                String oldId = group.getTimeTableId();
//
//                group = hTravelGroupMapper.selectByPrimaryKey(groupId);
//                String newId = group.getTimeTableId();
//                //指派的团不在同一个班期，且导游最近接的团不属于未出团，出团状态，才可以接团
//                if (!StringUtils.equals(oldId, newId)) {
//                    return ServerResponse.createByErrorMessage("指派的团不在同一个班期");
//                }
//                return this.insertHGuideTravelGroup(userId,guideId,groupId,guide);
//            //导游在线
//            case HConstant.D_GUIDE_STATUS_ON_LINE :
//                return this.insertHGuideTravelGroup(userId,guideId,groupId,guide);
//            case HConstant.D_GUIDE_STATUS_STOP:
//                return ServerResponse.createBySuccessMessage("导游账号已停用");
//        }
        //指派导游
        HGuideTravelGroup hGuideTravelGroup = new HGuideTravelGroup();
        hGuideTravelGroup.setId(FleaStringUtils.getGenerateUUID());
        hGuideTravelGroup.setCreateUser(userId);
        hGuideTravelGroup.setTravelGroupId(groupId);
        hGuideTravelGroup.setGuideId(guideId);
        //设置支付需要支付的费用
        hGuideTravelGroup.setGuideCost(guideCost);
        //设置支付部门
        hGuideTravelGroup.setPayDeptId(payDeptId);
        //设置支付状态 未支付
        hGuideTravelGroup.setStatus(HConstant.D_GUIDE_PAY_STATUS_NO);
        int row = hGuideTravelGroupMapper.insertSelective(hGuideTravelGroup);
        if (row == 1) {
            return ServerResponse.createBySuccessMessage("指派成功");
        }
        return ServerResponse.createByErrorMessage("指派失败");

    }

//    private ServerResponse<String> insertHGuideTravelGroup(String userId,String guideId,String groupId,HGuide guide) throws Exception {
//        //指派导游
//        HGuideTravelGroup hGuideTravelGroup = new HGuideTravelGroup();
//        hGuideTravelGroup.setId(FleaStringUtils.getGenerateUUID());
//        hGuideTravelGroup.setCreateUser(userId);
//        hGuideTravelGroup.setTravelGroupId(groupId);
//        hGuideTravelGroup.setGuideId(guideId);
//        int row = hGuideTravelGroupMapper.insertSelective(hGuideTravelGroup);
//        if (row == 1) {
//            //更新导游状态 为已接团 但未出发
//            guide.setStatus(HConstant.D_GUIDE_STATUS_RECEIVE);
//            guide.setModifyUser(userId);
//            row = hGuideMapper.updateByPrimaryKeySelective(guide);
//            if (row!=1){
//                throw new Exception("更新导致状态失败");
//            }
//            return ServerResponse.createBySuccessMessage("指派成功");
//        }
//        return ServerResponse.createByErrorMessage("指派失败");
//    }

    @Override
    public ServerResponse<String> updateStatusByUserId(String userId, String status) {
        HGuide guide = hGuideMapper.selectByUserId(userId);
        if (guide==null){
            return ServerResponse.createByErrorMessage("没有找到导游信息");
        }
        guide.setModifyUser(userId);
        guide.setStatus(status);
        int row = hGuideMapper.updateByPrimaryKeySelective(guide);
        if (row == 1) {
            return ServerResponse.createBySuccessMessage("更新状态成功");
        }
        return ServerResponse.createByErrorMessage("更新状态失败");
    }

    @Override
    public ServerResponse<String> updateAuditStatus(String userId, String guideId, String noPassCause) {
        HGuide entity = hGuideMapper.selectByPrimaryKey(guideId);
        if (entity == null) {
            return ServerResponse.createByErrorMessage("没有找到审核信息");
        }
        entity.setModifyUser(userId);
        //审核未通过时，需要填写原因
        if (StringUtils.isNotBlank(noPassCause)) {
            entity.setAuditStatus(Const.GuideAuditStatusEnum.NO_PASS.getCode());
            entity.setNotPassCause(noPassCause);
            entity.setStatus(HConstant.D_GUIDE_STATUS_REST);
        } else {
            entity.setNotPassCause(StringUtils.EMPTY);
            entity.setAuditStatus(Const.GuideAuditStatusEnum.PASSED.getCode());
            entity.setStatus(HConstant.D_GUIDE_STATUS_ON_LINE);
        }

        int row = hGuideMapper.updateByPrimaryKeySelective(entity);
        if (row != 1) {
            return ServerResponse.createByErrorMessage("操作失败");
        }
        HGuideAccount account = hGuideAccountMapper.selectByPrimaryKey(entity.getUserId());
        if (account == null) {
            return ServerResponse.createByErrorMessage("没有找到该用户注册信息");
        }
        String accountId = account.getId();
        try {
            //更新角色  你没有看错
            if (entity.getAuditStatus().equals(Const.GuideAuditStatusEnum.NO_PASS.getCode())) {
                boolean result = FleaOrgUtils.updateUserRole(accountId, HConstant.HCLV_GUAI_CONFIRM_WAIT_ROLE, HConstant.HCLV_GUAI_CONFIRM_NO_ROLE);
                if (!result){
                    throw new Exception("更新角色失败");
                }
            }else {
                boolean res2 = FleaOrgUtils.updateUserRole(accountId, HConstant.HCLV_GUAI_CONFIRM_WAIT_ROLE, HConstant.HCLV_GUAI_CONFIRM_YES_ROLE);
                if (!res2){
                    throw new Exception("更新角色失败");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (StringUtils.equals(entity.getAuditStatus(),Const.GuideAuditStatusEnum.PASSED.getCode())){
            String receEmail = account.getEmail();
            //发送审核通过邮件
            EmailUtil.builder()
                    .receive(receEmail)
                    .content("恭喜您，通过审核。接下来可以平台在需要导游时会电话联系您。")
                    .title("审核通过通知")
                    .build()
                    .sendEmail();
        }else {
            String receEmail = account.getEmail();
            //发送审核通过邮件
            EmailUtil.builder()
                    .receive(receEmail)
                    .content("未通过审核，不通过原因："+entity.getNotPassCause())
                    .title("未审核通过通知")
                    .build()
                    .sendEmail();
        }
        return ServerResponse.createBySuccessMessage("更新成功");
    }

    @Override
    public ServerResponse<FleaPage<HGuide>> selectAll(int curPage, int pageSize, String queryName, String queryCertificatesNumber, String queryAuditStatus, String queryProvince, String queryCity) {
        List<HAddress> addressList;
        List<String> addressIdList = Lists.newArrayList();
        if (StringUtils.isNotBlank(queryProvince) && StringUtils.isNotBlank(queryCity)) {
            //获取该地区下的导游
            addressList = hAddressMapper.selectByParams(queryProvince, queryCity, null);
            if (CollectionUtils.isNotEmpty(addressList)) {
                for (HAddress addressEntity : addressList) {
                    addressIdList.add(String.valueOf(addressEntity.getId()));
                }
            } else {
                return ServerResponse.createBySuccess(new FleaPage<HGuide>());
            }
        }

//        List<String> userIdList = Lists.newArrayList();
//        if (StringUtils.isNotBlank(queryName)) {
//            //获取该名字的人
//            List<OrgUser> users = orgUserMapper.findByLikeName(queryName);
//            if (CollectionUtils.isNotEmpty(users)) {
//                for (OrgUser user : users) {
//                    userIdList.add(user.getUserUuid());
//                }
//            } else {
//                return ServerResponse.createBySuccess(new FleaPage<HGuide>());
//            }
//        }


        RowBounds rowBounds = new RowBounds(curPage, pageSize);

        List<HGuide> hGuides = hGuideMapper.selectAll(rowBounds, queryName, queryCertificatesNumber, queryAuditStatus, addressIdList);
        FleaPage<HGuide> fleaPage = new FleaPage<>(hGuides);
        return ServerResponse.createBySuccess(fleaPage);
    }

    @Override
    public ServerResponse<String> saveGuideInfo(String userId, String model) {
        if (StringUtils.isBlank(model)) {
            return ServerResponse.createByErrorMessage("model为空");
        }
        HGuide hGuide = JSON.parseObject(model, HGuide.class);
        if (StringUtils.isNotEmpty(hGuide.getId())){
            return this.updateGuideInfo(userId,model);
        }
        //导游的地址
        HAddress hAddress = JSON.parseObject(model, HAddress.class);
        if (StringUtils.isEmpty(hAddress.getProvince())) {
            return ServerResponse.createByErrorMessage("省份为空");
        }
        if (StringUtils.isEmpty(hAddress.getCity())) {
            return ServerResponse.createByErrorMessage("城市为空");
        }
        hAddress.setId(FleaStringUtils.getGenerateUUID());
        hAddress.setCreateUser(userId);
        int row = hAddressMapper.insertSelective(hAddress);
        if (row != 1) {
            return ServerResponse.createByErrorMessage("导游地址添加失败");
        }
        //导游其他信息

        hGuide.setId(FleaStringUtils.getGenerateUUID());
        hGuide.setCreateUser(userId);
        hGuide.setUserId(userId);
        hGuide.setRegisterArea(hAddress.getId());
        // 设置审核状态 未审核
        hGuide.setAuditStatus(Const.GuideAuditStatusEnum.UN_AUDIT.getCode());
        //设置状态 闲置 表示可以接团
        hGuide.setStatus(HConstant.D_GUIDE_STATUS_ON_LINE);
        row = hGuideMapper.insertSelective(hGuide);
        if (row != 1) {
            return ServerResponse.createByErrorMessage("导游相关信息添加失败");
        }
        return ServerResponse.createBySuccessMessage("审核已提交");
    }
}
